package com.hazardSoft.eStore.bussines;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.hazardSoft.eStore.common.exception.SystemException;
import com.hazardSoft.eStore.entities.Inventory;
import com.hazardSoft.eStore.entities.Office;
import com.hazardSoft.eStore.entities.Product;
import com.hazardSoft.eStore.utils.PathConstants;
import com.hazardSoft.eStore.utils.QueryConstants;

public class InventoryBO {

	private static final String CLASS_NAME = PathConstants.BUSSINES_PATH+InventoryBO.class.getSimpleName();
	
	public Inventory insertInventory(Inventory inventory, EntityManager entityManager)
			throws SystemException {
		if (this.searchInventory(inventory, entityManager) == null) {
			if (inventory.getId() == null) {
				inventory.setId(0);
			}
			entityManager.persist(inventory);
			return this.searchInventory(inventory, entityManager);
		}
		return null;
	}

	
	public List<Inventory> searchInventoriesByOffice(Office office, EntityManager entityManager)
			throws SystemException {
		if (office == null) {
			throw new SystemException(new Exception("null office"),CLASS_NAME,"searchInventoriesByOffice");
		}
		if (office.getId() == null) {
			throw new SystemException(new Exception("null office.id"),CLASS_NAME,"searchInventoriesByOffice");
		}
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_INVENTORIES_BY_OFFICE);
		query.setParameter(1, office);
		
		return query.getResultList();
	}

	
	public List<Inventory> searchInventoriesByProduct(Product product, EntityManager entityManager)
			throws SystemException {
		if (product == null) {
			throw new SystemException(new Exception("null product"),CLASS_NAME,"searchInventoriesByProduct");
		}
		if (product.getId() == null) {
			throw new SystemException(new Exception("null office.id"),CLASS_NAME,"searchInventoriesByProduct");
		}
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_INVENTORIES_BY_PRODUCT);
		query.setParameter(1, product);
		
		return query.getResultList();
	}

	
	public Inventory updateInventory(Inventory inventory, EntityManager entityManager)
			throws SystemException {
		Inventory tempInventory = this.searchInventory(inventory, entityManager);
		if (!(tempInventory == null)) {
			if (!(inventory.getQuantity() == null)) {
				tempInventory.setQuantity(inventory.getQuantity());
			}
			
			return tempInventory;
		}
		return null;
	}
	
	public Inventory searchInventory(Inventory inventory, EntityManager entityManager) throws SystemException{
		if (inventory == null) {
			throw new SystemException(new Exception("null inventory"),CLASS_NAME,"searchInventory");
		}
		if (inventory.getOffice() == null) {
			throw new SystemException(new Exception("null inventory.office"),CLASS_NAME,"searchInventory");
		}
		if (inventory.getProduct() == null) {
			throw new SystemException(new Exception("null inventory.product"),CLASS_NAME,"searchInventory");
		}
		
		Query query = entityManager.createNamedQuery(QueryConstants.QUERY_SEARCH_INVENTORY);
		query.setParameter(1, inventory.getProduct());
		query.setParameter(2, inventory.getOffice());
		
		List<Inventory> inventories = query.getResultList();
		
		if (inventories.size() > 0) {
			return inventories.get(0);
		}
		
		return null;
	}
	
}
